package boundary;

import java.awt.Choice;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JRadioButton;

import client.Client;
import entity.DisabledPlace;
import entity.ParkingLot;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;

public class DisableSaveActivatePlSpotGui extends JPanel{
	JFrame frame;
	JPanel lastPanel;
	private JTextField textField;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	
	public DisableSaveActivatePlSpotGui() {
		final JRadioButton rdbtnActivate = new JRadioButton("Activate");
		final JRadioButton rdbtnDisable = new JRadioButton("Disable");
		final JRadioButton rdbtnSave = new JRadioButton("Save");
		final JButton btnInitialize = new JButton("initialize");
		final Choice choice = new Choice();
		choice.setBounds(52, 96, 55, 22);
		add(choice);
		
		final Choice choice_1 = new Choice();
		choice_1.setBounds(158, 96, 55, 20);
		add(choice_1);
		
		final Choice choice_2 = new Choice();
		choice_2.setBounds(270, 96, 55, 20);
		add(choice_2);
		
		rdbtnDisable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				//if (rdbtnDisable.isSelected())
				//{
					textField.setEnabled(true);
					rdbtnSave.setSelected(false);
					rdbtnActivate.setSelected(false);
				//}
				
			}
		});
		rdbtnDisable.setBounds(61, 190, 109, 23);
		add(rdbtnDisable);
		
		
		rdbtnSave.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//if (rdbtnSave.isSelected())
				//{
					textField.setEnabled(false);
					rdbtnDisable.setSelected(false);
					rdbtnActivate.setSelected(false);
				//}
				
			}
		});
		rdbtnSave.setBounds(172, 190, 109, 23);
		add(rdbtnSave);
		
		textField = new JTextField();
		textField.setEnabled(false);
		textField.setBounds(104, 143, 217, 20);
		add(textField);
		textField.setColumns(10);
		
		
		rdbtnActivate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//if (rdbtnActivate.isSelected())
				//{
					textField.setEnabled(false);
					rdbtnSave.setSelected(false);
					rdbtnDisable.setSelected(false);
				//}
				
				
			}
		});
		rdbtnActivate.setBounds(283, 190, 109, 23);
		add(rdbtnActivate);
	
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
			}
		});
		setLayout(null);
		
	
	
	
	JLabel lblInitializeParkingLot = new JLabel("Parking lot: "+Client.getParkingLotNameByNumber(Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum()));
	lblInitializeParkingLot.setFont(new Font("Tahoma", Font.PLAIN, 18));
	lblInitializeParkingLot.setBounds(73, 11, 304, 22);
	add(lblInitializeParkingLot);
	
	
	
	
	JLabel lblLinesInStore = new JLabel("line in store");
	lblLinesInStore.setBounds(52, 76, 83, 14);
	add(lblLinesInStore);
	
	JLabel lblStores = new JLabel("store");
	lblStores.setBounds(172, 76, 55, 14);
	add(lblStores);
	
	JLabel lblColsInStore = new JLabel("col in store");
	lblColsInStore.setBounds(270, 76, 76, 14);
	add(lblColsInStore);
	
	
	btnInitialize.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			int place []=new int[3];
			place[0]=Integer.valueOf(choice.getSelectedItem());
			place[1]=Integer.valueOf(choice_1.getSelectedItem());
			place[2]=Integer.valueOf(choice_2.getSelectedItem());
			ParkingLot pl=Client.getParkingLots_sorted()[Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum()-1];
			if (place[0]<3 && place[1]<3 && place[2]<pl.getCols())
			{
			
			if (rdbtnDisable.isSelected())
			{
				if (pl.disableSpot(place[0], place[1], place[2]))
				{
					DisabledPlace dp=new DisabledPlace(pl.getParking_lot_number(),place,textField.getText());
					dp.setStart(new Date());
					dp.save();
					JOptionPane.showMessageDialog(null,"parking spot disabled","save parking spot",JOptionPane.INFORMATION_MESSAGE);
					frame.setContentPane((JPanel)lastPanel);
				}
				else
					JOptionPane.showMessageDialog(null,"this parking spot isnt free","save parking spot",JOptionPane.ERROR_MESSAGE);
			}
			
			else if (rdbtnSave.isSelected())
			{
				if (pl.saveSpot(place[0], place[1], place[2]))
				{
					JOptionPane.showMessageDialog(null,"parking spot saved","save parking spot",JOptionPane.INFORMATION_MESSAGE);
					frame.setContentPane((JPanel)lastPanel);
				}
				else
					JOptionPane.showMessageDialog(null,"this parking spot isnt free","save parking spot",JOptionPane.ERROR_MESSAGE);

			}
			
			else if (rdbtnActivate.isSelected())
			{
				if (pl.activateSpot(place[0], place[1], place[2]))
				{
					DisabledPlace dp=Client.getCurrentDisabledParkingSpot(place,Client.getPlWorkerByID(Client.getLoggedIn()).getParkingLotNum());
					if (dp!=null)
					{
						dp.setEnd(new Date());
						Client.updateDisabledSpot(dp);
					}
					JOptionPane.showMessageDialog(null,"parking spot activated","save parking spot",JOptionPane.INFORMATION_MESSAGE);
					frame.setContentPane((JPanel)lastPanel);
				}
				else
					JOptionPane.showMessageDialog(null,"somthing is wronge, call johnson for tech support","save parking spot",JOptionPane.ERROR_MESSAGE);
			
			}
			else
				JOptionPane.showMessageDialog(null,"choose one of the options","save/disable/activate",JOptionPane.ERROR_MESSAGE);
			}
			else
				JOptionPane.showMessageDialog(null,"illegal parking spot!","save/disable/activate",JOptionPane.ERROR_MESSAGE);
			
			
		}
	});
	btnInitialize.setBounds(73, 240, 89, 23);
	add(btnInitialize);
	
	JButton btnCancel = new JButton("cancel");
	btnCancel.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			frame.setContentPane((JPanel)lastPanel);
		}
	});
	btnCancel.setBounds(257, 240, 89, 23);
	add(btnCancel);
	
	JLabel lblReason = new JLabel("reason: ");
	lblReason.setBounds(61, 141, 55, 24);
	add(lblReason);
	
	
	
	
	for (int i=0; i<10; i++)
	{
		choice.add(String.valueOf(i));
		choice_1.add(String.valueOf(i));
		choice_2.add(String.valueOf(i));
	}
}
}
